package com.otitan.la.forest.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.otitan.la.forest.system.constant.DicEnum;
import com.otitan.la.forest.system.dto.PermissionDTO;
import com.otitan.la.forest.system.entity.PermissionEntity;
import com.otitan.la.forest.system.mapper.PermissionMapper;
import com.otitan.la.forest.system.service.DicService;
import com.otitan.la.forest.system.service.PermissionService;
import com.otitan.la.forest.system.vo.PermissionPageVO;
import com.otitan.webapp.framework.basepro.exception.code.BaseResponseCode;
import com.otitan.webapp.framework.basepro.model.DataResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, PermissionEntity> implements PermissionService {

    @Autowired
    private DicService dicService;
    @Resource
    private PermissionMapper permissionMapper;

    @Override
    public DataResult add(PermissionEntity permission, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return DataResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        boolean isSuccess = this.save(permission);
        return isSuccess ? DataResult.success() : DataResult.fail(BaseResponseCode.DATA_NOT_EXIST.getMsg());
    }

    @Override
    public DataResult batchDelete(List<String> ids) {
        boolean isSuccess = this.removeByIds(ids);
        log.debug("\n>>>>>>>>> 【删除动作元素表】 <<<<<<<<<\n操作结果：{}，ids：{}\n>>>>>>>>> ---- <<<<<<<<<", isSuccess, ids);
        return DataResult.success();
    }

    @Override
    public DataResult editor(PermissionEntity permission, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return DataResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        boolean isSuccess = this.updateById(permission);
        return isSuccess ? DataResult.success() : DataResult.fail(BaseResponseCode.DATA_NOT_EXIST.getMsg());
    }

    @Override
    public DataResult findPageData(Long page, Long size, PermissionDTO permissionDTO) {
        Page<PermissionPageVO> pages=new Page<>(page,size);
        Page<PermissionPageVO> entityPage=pages.setRecords(permissionMapper.findPageData(permissionDTO,pages));
        List<PermissionPageVO> permissionEntityVOList=entityPage.getRecords();
        if(CollUtil.isNotEmpty(permissionEntityVOList)){
            permissionEntityVOList.forEach(permission->{
                String typeName=dicService.getDicName(DicEnum.BUTTON_TYPE.getValue(),permission.getType());
                permission.setTypeName(typeName);
            });
        }
        return DataResult.success(entityPage);
    }

    @Override
    public DataResult findOne(String id) {
        PermissionEntity permissionEntity=this.getById(id);
        PermissionPageVO permissionPageVO = new PermissionPageVO();
        BeanUtils.copyProperties(permissionEntity, permissionPageVO);
        String typeName=dicService.getDicName(DicEnum.BUTTON_TYPE.getValue(),permissionPageVO.getType());
        permissionPageVO.setTypeName(typeName);
        return DataResult.success(permissionPageVO);
    }



}